package org.jili.novoland.listener;

import org.jili.novoland.game.ResourceManager;

import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;

public class DragListener implements OnTouchListener {
    ResourceManager resourceManager;
    private int lastX, lastY;
    private boolean isDrag = false;

    public DragListener(ResourceManager resourceManager) {
        super();
        this.resourceManager = resourceManager;
    }

    public boolean onTouch(View v, MotionEvent event) {
        int deltaX, deltaY;
        switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            lastX = (int) event.getRawX();
            lastY = (int) event.getRawY();
            isDrag = false;
            break;
        case MotionEvent.ACTION_MOVE:
            deltaX = (int) event.getRawX() - lastX;
            deltaY = (int) event.getRawY() - lastY;
            lastX = (int) event.getRawX();
            lastY = (int) event.getRawY();

            resourceManager.getBattleFieldMap().move(deltaX, deltaY);
            // resourceManager.getLayerManager().paint(resourceManager., x, y)
            // Log.d(DragListener.class.toString(),"deltaX:" +
            // Integer.toString(deltaX) + ", deltaY:" +
            // Integer.toString(deltaY));
            isDrag = true;
            break;
        case MotionEvent.ACTION_UP:
            if (!isDrag) {
                resourceManager.getCurrentRole().moveTo((int) event.getRawX(),
                        (int) event.getRawY());
            }
            isDrag = false;
            break;
        default:
            deltaX = 0;
            deltaY = 0;
            isDrag = false;
            break;
        }

        return true;
    }

}
